package com.wrupple.muba.catalogs.client.module.services.logic.impl;

import java.util.Arrays;
import java.util.List;

import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.event.shared.EventBus;
import com.google.inject.Inject;
import com.wrupple.muba.catalogs.client.module.services.logic.EntryCreationService;
import com.wrupple.muba.catalogs.client.module.services.logic.EntryDeleteService;
import com.wrupple.muba.catalogs.client.module.services.logic.EntryRetrivingService;
import com.wrupple.muba.catalogs.client.module.services.logic.EntryUpdateService;
import com.wrupple.muba.catalogs.domain.CatalogEntry;
import com.wrupple.muba.common.domain.FilterCriteria;
import com.wrupple.muba.common.domain.FilterData;
import com.wrupple.muba.common.shared.State.StorageManager;
import com.wrupple.muba.common.shared.StateTransition;
import com.wrupple.muba.common.shared.Transaction;

/**
 * Entry Point for the Muba Storage Managemente System
 * 
 * @author japi
 *
 */
public class CatalogStorageManager implements StorageManager {
	
	private EntryDeleteService deleteService;
	private EntryCreationService creationService;
	private EntryUpdateService updateService;
	private EntryRetrivingService retrivingService;
	private EventBus eventBus;
	
	@Inject
	public CatalogStorageManager(EntryDeleteService deleteService, EntryCreationService creationService,
			EntryUpdateService updateService, EntryRetrivingService retrivingService,EventBus eventBus) {
		super();
		this.deleteService = deleteService;
		this.creationService = creationService;
		this.updateService = updateService;
		this.retrivingService = retrivingService;
		this.eventBus = eventBus;
	}

	@Override
	public Transaction startTransaction() {
		throw new UnsupportedOperationException();
	}

	@Override
	public void create(String type, JavaScriptObject entry, StateTransition<? extends JavaScriptObject> callback) {
		CatalogEntry data = entry.cast();
		String catalogid = type;
		StateTransition<? extends JavaScriptObject> onDone = callback;
		creationService.createEntry(data, catalogid, onDone, eventBus );
	}

	@Override
	public void read(String type, String id, StateTransition<? extends JavaScriptObject> callback) {
		retrivingService.retriveEntryById(id, type, callback, eventBus);
	}


	@Override
	public void update(String type, String id, JavaScriptObject entry,
			StateTransition<? extends JavaScriptObject> callback) {
		assert id!=null;
		CatalogEntry data = entry.cast();
		updateService.updateEntry(data, id, type, callback, eventBus);
	}

	@Override
	public void delete(String type, String id, StateTransition<? extends JavaScriptObject> callback) {
		deleteService.deleteEntry(id, type, (StateTransition<CatalogEntry>) callback, eventBus);
	}


	//FIXME concentrate generic warnings in the storage manager then fix it all at once
	@Override
	public <T extends JavaScriptObject> void read(String type, FilterData filter, StateTransition<List<T>> callback) {
		StateTransition cb = callback;
		retrivingService.retriveFilteredEntries(type, filter, cb , eventBus);
	}
	
	
	@Override
	public <T extends JavaScriptObject> void summary(String type,
			FilterData filter, StateTransition<List<T>> callback) {
		StateTransition cb = callback;
		retrivingService.retriveFilteredSummaryEntries(type, filter, cb , eventBus);
	}


	@Override
	public <T extends JavaScriptObject> void read(String catalog,
			List<String> ids, StateTransition<List<T>> callback) {
		
		FilterData filter = new FilterData();
		FilterCriteria criteria = new FilterCriteria();
		criteria.setOperator(FilterData.EQUALS);
		criteria.setValues(ids);
		filter.getFilters().put("id", Arrays.asList(criteria ));
		
		read(catalog, filter, callback);
	}


}
